package com.cheeven.amazonbooksreviewscores;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;

public class GetPage {

    Tidy tidy;
    XPath xpath;

    public GetPage() {
        tidy = new Tidy();
        tidy.setQuiet(true);
        tidy.setShowWarnings(false);
        tidy.setWraplen(20000);
        //tidy.setForceOutput(true);

        xpath = XPathFactory.newInstance().newXPath();
    }

    public Document getPageDOM(URL url) throws Exception {
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();

        File logFolder = new File("logs");
        if (!logFolder.exists()) {
            logFolder.mkdirs();
        }
        Document doc = tidy.parseDOM(in, null);
        //Document doc = Jsoup.connect(url.toString()).get();
        in.close();

        File logFile = new File(logFolder, System.currentTimeMillis() + ".html");
        FileOutputStream logOut = new FileOutputStream(logFile);
        tidy.pprint(doc, logOut);
        logOut.close();

        return doc;
    }

    public String getPageInfo(Node node, String xpathExpression) throws XPathExpressionException {
        return xpath.evaluate(xpathExpression, node);
    }

    public Element getPageInfoElement(Node node, String xpathExpression) throws XPathExpressionException {
        return (Element) xpath.evaluate(xpathExpression, node, XPathConstants.NODE);
    }

    public NodeList getPageInfoNodeList(Node node, String xpathExpression) throws XPathExpressionException {
        return (NodeList) xpath.evaluate(xpathExpression, node, XPathConstants.NODESET);
    }
}
